package.cpath = "./skynet/luaclib/?.so;".. package.cpath
package.path = "./skynet/lualib/?.lua;./app/common/lib/?.lua;./testclient/?.lua;" .. package.path

if _VERSION ~= "Lua 5.3" then
	error "Use lua 5.3"
end

local socket = require "client.websocket"
local protobuf = require "protobuf"
local parser = require "parser"

local md5 = require "md5"

parser.register("./app/proto/primary.proto")
parser.register("./app/mjzznode/mjzz.proto")

local decode = require "protopack"

-- local fd = assert(socket.connect("192.168.6.202", 8889))
local fd = assert(socket.connect("127.0.0.1", 8890))
-- local fd = assert(socket.connect("192.168.6.81", 8890))

local function send_package(fd, pack)
	-- 使用了websocket ，会自己组织帧格式的
	socket.send(fd, pack)
end

local function recv_package()
	local r , istimeout= socket.recv(fd, 100)
	if not r then
		return nil
	end
	if r == ""  and (not istimeout ) then
		error "Server closed"
	end
	return r
end


local function send_request(msgname, msg)
	prints(msgname)
	prints(msg)
	local str = decode.c2spack(protobuf, msgname, msg)
	send_package(fd, str)
end

function prints(...)
	local args = {...}
	local records = {}
	local function _print(k, v, tm)
		if type(k) ~= "string" and type(k) ~= "number" then
			if not tostring(k) then
				k = "-unknown-key-"
			end
		end

		local tab = ""
		for i = 1, tm do
			tab = tab .. "    "
		end
		if type(v) == "string" then
			if type(k) == type("") then
				print(tab .. '["' .. k .. '"]' .. " = " .. '"' .. v .. '",')
			else
				print(tab .. "[" .. tostring(k) .. "]" .. " = " .. '"' .. v .. '",')
			end
		elseif type(v) == "number" then
			if type(k) == type("") then
				print(tab .. '["' .. k .. '"]' .. " = " .. v .. ",")
			else
				print(tab .. "[" .. tostring(k) .. "]" .. " = " .. v .. ",")
			end
		elseif type(v) == "boolean" then
			if type(k) == type("") then
				print(tab .. '["' .. k .. '"]' .. " = " .. tostring(v) .. ",")
			else
				print(tab .. "[" .. tostring(k) .. "]" .. " = " .. tostring(v) .. ",")
			end
		elseif type(v) == "table" then
			if records[v] then	-- recursive table check!
				if type(k) == type("") then
					print(tab .. '["' .. k .. '"]' .. " = " .. "[RECURSIVE TABLE],")
				else
					print(tab .. "[" .. tostring(k) .. "]" .. " = " .. "[RECURSIVE TABLE],")
				end
			else
				records[v] = true
				if type(k) == type("") then
					print(tab .. '["' .. k .. '"]' .. " = " .. "{")
				else
					print(tab .."[" .. tostring(k) .. "]" .. " = " .. "{")
				end
				for _k, _v in pairs(v) do
					_print(_k, _v, tm + 1)
				end
				print(tab .. "},")
			end
		else
			if type(k) == type("") then
				print(tab .. '["' .. k .. '"]' .. " = " .. tostring(v) or "UNKNOWN,")
			else
				print(tab .. "[" .. tostring(k) .. "]" .. " = " .. tostring(v) or "UNKNOWN,")
			end
		end
	end
	for i, v in ipairs(args) do
		if type(v) == type{} then
			records[v] = true
			print("{")
			for k, v in pairs(v) do
				_print(k, v, 1)
			end
			print("}")
		else
			print(v)
		end
		if args[i + 1] then
			print("\n")
		end
	end
end
local function print_response(msghead, msgbody)
	print('------ begin response --------')
	if msghead then
		print("msghead")
		prints(msghead)
	end
	if msgbody then
		print("msgbody")
		prints(msgbody)
	end
	print('------ end response --------')
end

local function dispatch_package()
	while true do
		local v
		v = recv_package()
		if not v  or v == "" then
			break
		end
		print_response(decode.s2cunpack(protobuf, v))
	end
end
-- send_request("login", {uid=1234, logintoken='46634e1c0013cdea0172e1fab15boc94b1504418301'})
local rid = 7061
local tokentime = os.time()
local gatetoken = md5.sumhexa(rid .. tokentime .. "l2g_salt" ) .. tokentime
send_request("entergame", {rid=rid, uid=1,expiretime=0, gatetoken=gatetoken, agencyrid=222})

-- socket.usleep(2000)
-- send_request("intogame", 1, { rid = 1 })
while true do
	dispatch_package()
	local cmd = socket.readstdin()
	if cmd then
		if cmd == "hi" then
			send_request("hi", {test='rolenameabc'})
		elseif cmd == "1" then
			send_request("mjzz.createtable", {table_conf={table_type=1, 
														max_player_num=4,
														hu_dianpao_zimo=1,
														retain_to_num = 8,
														is_zhuangxian=0,
														is_qidui=0,
														is_hongzhong=0,
														zhuaniao=0}})
		elseif cmd == "2" then
			send_request("gettablebycode", {code=123456})
		elseif cmd == "3" then
			send_request("mjzz.entertable", {table_index=1})
		elseif cmd == "4" then
			send_request("mjzz.sitdowntable", {})
		elseif cmd == "5" then
			send_request("mjzz.standuptable", {table_index=1})
		elseif cmd == "6" then
			send_request("mjzz.leavetable", {table_index=1})
		elseif cmd == "heart" then
			send_request("heart", {})
		elseif cmd == "qiniu" then
			send_request("getqiniutoken", {uploadstr="jyqp-icon:1_2_Avatar_22144_.jpg"})
		elseif cmd == "11" then
			send_request("getroleinfo", {})
		elseif cmd == "12" then
			send_request("updateroleinfo", {rolename="abc"})
		elseif cmd == "13" then
			send_request("getpubtablelist", {gamename="mjzz"})
		elseif cmd == "14" then
			send_request("getgameresult", {resulttype=1})
		elseif cmd == "15" then
			send_request("getgameresult", {resulttype=2})
		elseif cmd == "16" then
			send_request("gethrlconfigmsg", {})
		elseif cmd == "17" then
			send_request("tablechat", {objrid=0, chattype=1, content="aaaaaaaaa", ridlist={7061,7062}})
		elseif cmd == "18" then
			send_request("getrank", {})
		elseif cmd == "19" then
			send_request("getidlerole", {})
		elseif cmd == "21" then
			send_request("banksave", {value=1})
		elseif cmd == "22" then
			send_request("bankwithdraw", {value=1})
		elseif cmd == "23" then
			send_request("tableinvite", {gamenode="abcnode", table_index=1, conf_intro="aaaaaaaaa", ridlist={7061,7062}})
		elseif cmd == "24" then
			send_request("getmaillist", {})
		elseif cmd == "25" then 
			send_request("getmailattach", {keyid=1511247385562})
		elseif cmd == "26" then
			send_request("delmail", {keyid=1511256493802})
		elseif cmd == "27" then
			send_request("gethallad", {})
		elseif cmd == "hello" then
			send_request("hello", {test='rolenameabc'})
		elseif cmd == "tab" then
			send_request("gone.tab", 123, {key='getkeyabc'})
		elseif cmd == "quitgame" then
			send_request("quitgame", {}, 1)
		end
	else
		socket.usleep(100)
	end
end
